package com.yijiu.trb;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

import com.yijiu.resource.CacheData;
import com.yijiu.view.MyProgressDialog;

public class MainTabActivity extends TabActivity implements OnCheckedChangeListener
{
	public static final String TAG = "MainTabActivity";
	public TabHost mHost;
	private RadioButton rbProject;
	private RadioButton rbAccount;
	private RadioButton rbOperation;
	private RadioButton rbMore;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_tab);
		mHost = this.getTabHost();
		// 推荐项目
		mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE").setContent(new Intent(this, SuggestProjectActivity.class)));
		// 我的记录
		mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO").setContent(new Intent(this, MyRecordActivity.class)));
		// 账户管理
		mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE").setContent(new Intent(this, AccountManagerActivity.class)));
		// 更多
		mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR").setContent(new Intent(this, MoreActivity.class)));
		RadioGroup radioderGroup = (RadioGroup) findViewById(R.id.rg_main_tab);
		rbProject = (RadioButton) findViewById(R.id.rb_main_tab_suggest_project);
		rbAccount = (RadioButton) findViewById(R.id.rb_main_tab_my_account);
		rbOperation = (RadioButton) findViewById(R.id.rb_main_tab_account_operation);
		rbMore = (RadioButton) findViewById(R.id.rb_main_tab_more);
		radioderGroup.setOnCheckedChangeListener(this);
		init("ONE", rbProject);
	}
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId)
	{
		switch (checkedId)
		{
			case R.id.rb_main_tab_suggest_project:
				SuggestProjectActivity.instance.initWidget();
				init("ONE", rbProject);
				break;
			case R.id.rb_main_tab_my_account:
				if (MyRecordActivity.instance != null) {
					MyRecordActivity.instance.initWidget();
					init("TWO", rbAccount);
				} else
					checkIsLogin("TWO", rbAccount);
				break;
			case R.id.rb_main_tab_account_operation:
				checkIsLogin("THREE", rbOperation);
				break;
			case R.id.rb_main_tab_more:
				init("FOUR", rbMore);
				break;
		}
	}
	private void checkIsLogin(String flag, RadioButton rb)
	{
		if (CacheData.LOGIN_STATUS)
		{
			init(flag, rb);
		}
		else
		{
			Intent intent = new Intent();
			intent.setClass(this, LoginActivity.class);
			intent.putExtra("TAG", TAG);
			startActivity(intent);
			MyProgressDialog dialog = new MyProgressDialog(this);
			if (dialog.isShowing())
			{
				dialog.dismiss();
			}
			finish();
		}
	}
	private void init(String flag, RadioButton rb)
	{
		mHost.setCurrentTabByTag(flag);
		rb.setChecked(true);
	}
}
